package com.financing.controller.invest;
import java.util.LinkedList;
import java.util.Map;

import com.financing.common.CONST;
import com.financing.enums.invest.CouponRule;
import com.financing.model.Data;
import com.financing.model.invest.Coupon;
import com.financing.model.invest.CouponLog;
import com.financing.model.invest.Trade;
import com.financing.model.user.User;
import com.financing.service.invest.CouponLogService;
import com.financing.service.user.UserService;
import com.financing.util.Excel;
import com.financing.util.Json;
import com.financing.util.Money;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class CouponLogController extends Controller {
	private UserService user = new UserService();
	private CouponLogService couponLog = new CouponLogService();

	/**
	 * 我的优惠券记录
	 */
	public void myList() {
		User u = getAttr("user");
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		
		// 获取数据 
		Map<String, String> params = Data.flat(getParaMap());
		params.put("userId", String.valueOf(u.getId()));
		params.put("isShow", "1");
		Page<CouponLog> p  = couponLog.list(page, size, params);
		
		// 优惠券条件名称转换
		couponLog.transTagName(p.getList());

		renderJson(Json.ok("获取数据成功！",Data.put("page",p, "xjhb", couponLog.sumByType(u, "xjhb"), "tzhb", couponLog.sumByType(u, "tzhb"))));
	}	
	 
	/**
	 * 清除失效优惠券
	 */
	public void clear() {
		User u = getAttr("user"); 
		couponLog.clearByType(u.getId());
		renderJson(Json.ok("失效优惠券已清除！"));
	}
	
	/**
	 * 优惠券记录列表
	 */
	public void list() { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Map<String, String> params = Data.flat(getParaMap());
		Page<CouponLog> p = couponLog.list(page,size,params);
		for(CouponLog cl : p.getList()){
			if(Str.ne("xjhb",cl.getType())) {
				cl.put("coupon", Coupon.me.findById(cl.getCouponId()));
				cl.put("trade", Trade.me.findById(cl.getTradeId()));
			}
		}
		renderJson(Json.ok("数据获取成功！", Data.put("page",p)));
	}
	
	/**
	 * 定向发放
	 * @throws Exception 
	 */
	@SuppressWarnings("rawtypes")
	public void direct() throws Exception { 

		User operator = getAttr("user");
		String type = getPara("type", "coupon");
		String mode = getPara("mode", "single");
		Coupon c = Coupon.me.findById(getParaToInt("couponId",0));
			
		if((Str.eq("coupon", type) && Str.eq(CouponRule.DIRECT, c.getRule())) || Str.eq("cash", type)){
			
			// 单人模式
			if(Str.eq("single", mode)){
				if(Str.eq("coupon", type)) {
					if(couponLog.directSend(c, getPara("amount", "0"), getParaToInt("userId",0), getPara("remark"))) {
						renderJson(Json.ok("优惠券发放成功！"));
					} else {
						renderJson(Json.error("优惠券数量不足！"));
					}
				} else if(Str.eq("cash", type)) {
					User u = User.me.findById(getParaToInt("userId",0));
					if(u!=null && u.getIsBindBankCard()==1) {
						if(couponLog.cashSend(getPara("amount", "0"), getParaToInt("userId",0), operator, getPara("remark"))) {
							renderJson(Json.ok("现金红包发放成功！"));
						} else {
							renderJson(Json.error("现金红包发放失败！"));
						}
					} else {
						renderJson(Json.error("用户未实名绑卡，不可发放现金奖励！"));
					}
				}
			// 批量模式
			}else if(Str.eq("multiple", mode)){
				if(Str.nb(getPara("file"))){
					Excel poi = new Excel();
					LinkedList[] result = poi.loadExcel(CONST.ROOT+getPara("file"));
					// 读取数据
					if(Str.eq("coupon", type)){ 	
						double left = Money.sub(c.getNum(), c.getApplyNum());
						if(left>=result.length) {
						} else {
							renderJson(Json.error("优惠券数量不足！"));
							return;
						}
					}
					// 开始循环发放
					for (int i = 0; i < result.length; i++) {
						if(result[i].size()<=0) {continue;}
						User u = user.findByMobile((String) result[i].get(0));
						if(u==null) {continue;}
						if(Str.eq("coupon", type)){
							couponLog.directSend(c, (String) result[i].get(1), u.getId(), getPara("remark"));
						}else if(Str.eq("cash", type)){
							if(u.getIsBindBankCard()==1) {
								couponLog.cashSend((String) result[i].get(1), u.getId(), operator, getPara("remark"));
							}
						}
						renderJson(Json.ok("优惠券批量发放成功！"));
					}
				}else{
					renderJson(Json.error("Excel文件数据不可为空！"));
				}
			}
		}else{
			renderJson(Json.error("类型不正确！"));
		}
	}
}
 